/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "arm_math.h"
#include "uart_print.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
static void DSP_ABS(void)
{
  float32_t pSrc;
  float32_t pDst;
  q31_t pSrc1;
  q31_t pDst1;
  q15_t pSrc2;
  q15_t pDst2;
  q7_t pSrc3;
  q7_t pDst3;

  pSrc -= 1.23f;
  arm_abs_f32(&pSrc, &pDst, 1);
  uart_print("arm_abs_f32 = %f\r\n", pDst);
  pSrc1 -= 1;
  arm_abs_q31(&pSrc1, &pDst1, 1);
  uart_print("arm_abs_q31 = %d\r\n", pDst1);
  pSrc2 = -32768;
  arm_abs_q15(&pSrc2, &pDst2, 1);
  uart_print("arm_abs_q15 = %d\r\n", pDst2);
  pSrc3 = 127;
  arm_abs_q7(&pSrc3, &pDst3, 1);
  uart_print("arm_abs_q7 = %d\r\n", pDst3);
  uart_print("***********************************\r\n");
}

static void DSP_Add(void)
{
  float32_t pSrcA;
  float32_t pSrcB;
  float32_t pDst;
  q31_t pSrcA1;
  q31_t pSrcB1;
  q31_t pDst1;
  q15_t pSrcA2;
  q15_t pSrcB2;
  q15_t pDst2;
  q7_t pSrcA3;
  q7_t pSrcB3;
  q7_t pDst3;

  pSrcA = 2.1432f;
  pSrcB = 1.2134f;
  arm_add_f32(&pSrcA, &pSrcB, &pDst, 1);
  uart_print("arm_add_f32 = %f\r\n", pDst);
  pSrcA1 = 1;
  pSrcB1 = 1;
  arm_add_q31(&pSrcA1, &pSrcB1, &pDst1, 1);
  uart_print("arm_add_q31 = %d\r\n", pDst1);
  pSrcA2 = 2;
  pSrcB2 = 2;
  arm_add_q15(&pSrcA2, &pSrcB2, &pDst2, 1);
  uart_print("arm_add_q15 = %d\r\n", pDst2);
  pSrcA3 = 30;
  pSrcB3 = 120;
  arm_add_q7(&pSrcA3, &pSrcB3, &pDst3, 1);
  uart_print("arm_add_q7 = %d\r\n", pDst3);
  uart_print("***********************************\r\n");
}

static void DSP_DotProduct(void)
{
  float32_t pSrcA[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t pSrcB[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t result;
  q31_t pSrcA1[5] = {0x7ffffff0,1,1,1,1};
  q31_t pSrcB1[5] = {1,1,1,1,1};
  q63_t result1;
  q15_t pSrcA2[5] = {1,1,1,1,1};
  q15_t pSrcB2[5] = {1,1,1,1,1};
  q63_t result2;
  q7_t pSrcA3[5] = {1,1,1,1,1};
  q7_t pSrcB3[5] = {1,1,1,1,1};
  q31_t result3;

  arm_dot_prod_f32(pSrcA, pSrcB, 5, &result);
  uart_print("arm_dot_prod_f32 = %f\r\n", result);
  arm_dot_prod_q31(pSrcA1, pSrcB1, 5, &result1);
  uart_print("arm_dot_prod_q31 = %ld\r\n", result1);
  arm_dot_prod_q15(pSrcA2, pSrcB2, 5, &result2);
  uart_print("arm_dot_prod_q15 = %ld\r\n", result2);
  arm_dot_prod_q7(pSrcA3, pSrcB3, 5, &result3);
  uart_print("arm_dot_prod_q7 = %d\r\n", result3);
  uart_print("***********************************\r\n");
}

static void DSP_Multiplication(void)
{
  float32_t pSrcA[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t pSrcB[5] = {1.0f,1.0f,1.0f,1.0f,1.0f};
  float32_t pDst[5];
  q31_t pSrcA1[5] = {1,1,1,1,1};
  q31_t pSrcB1[5] = {1,1,1,1,1};
  q31_t pDst1[5];
  q15_t pSrcA2[5] = {1,1,1,1,1};
  q15_t pSrcB2[5] = {1,1,1,1,1};
  q15_t pDst2[5];
  q7_t pSrcA3[5] = {0x70,1,1,1,1};
  q7_t pSrcB3[5] = {0x7f,1,1,1,1};
  q7_t pDst3[5];

  pSrcA[0] += 1.1f;
  arm_mult_f32(pSrcA, pSrcB, pDst, 5);
  uart_print("arm_mult_f32 = %f\r\n", pDst[0]);
  pSrcA1[0] += 1;
  arm_mult_q31(pSrcA1, pSrcB1, pDst1, 5);
  uart_print("arm_mult_q31 = %d\r\n", pDst1[0]);
  pSrcA2[0] += 1;
  arm_mult_q15(pSrcA2, pSrcB2, pDst2, 5);
  uart_print("arm_mult_q15 = %d\r\n", pDst2[0]);
  pSrcA3[0] += 1;
  arm_mult_q7(pSrcA3, pSrcB3, pDst3, 5);
  uart_print("arm_mult_q7 = %d\r\n", pDst3[0]);
  uart_print("***********************************\r\n");
}
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  DSP_ABS();
  DSP_Add();
  DSP_DotProduct();
  DSP_Multiplication();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 432;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Activate the Over-Drive mode
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
